package com.project.personnel.controller;


import com.project.personnel.dao.EmployeesalaryMapper;
import com.project.personnel.entity.Employeesalary;
import com.project.personnel.service.EmployeesalaryService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author diaominyh
 * @since 2022-12-31
 */
@RestController
@RequestMapping("/employeesalary")
public class EmployeesalaryController {
    @Resource
    EmployeesalaryService employeesalaryService;
    @Resource
    EmployeesalaryMapper employeesalaryMapper;

    /**
     * 查找所有的的员工实际薪资
     * @param id
     * @return
     */
    @RequestMapping("/getAll")
    public List<Employeesalary> getAll(Integer id){
        return employeesalaryService.list();
    }

    /**
     * 根据id查询员工的实际薪资
     * @param id
     * @return
     */
    @GetMapping("/getEmployeesalaryUserById/{id}")
    public Employeesalary getEmployeesalaryUserById(@PathVariable Integer id){
        return employeesalaryMapper.selectById(id);
    }

    /**
     * 修改员工实际薪资
     * @param employeesalary
     * @return
     */
    @PutMapping("/getEmplooyeesalaryUserUpd")
    public Boolean getEmpsalaryUserUpd(@RequestBody Employeesalary employeesalary ){
        return employeesalaryService.updateById(employeesalary);
    }

    @RequestMapping("/getemployeesalaryEX")
    public void employeesalaryAll(HttpServletResponse response,@RequestParam("id") List<Integer> id){
        List<Employeesalary> employeesalaryList = employeesalaryService.list();
        List<Employeesalary> employeesalaryList1 = (List<Employeesalary>) employeesalaryService.listByIds(id);//查询批量数据
        HSSFWorkbook hwd = new HSSFWorkbook();
        HSSFSheet sheet = hwd.createSheet("员工薪资");
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("编号");
        cell = row.createCell(1);
        cell.setCellValue("员工姓名");
        cell = row.createCell(2);
        cell.setCellValue("员工工号");
        cell = row.createCell(3);
        cell.setCellValue("实际工资");
        if (id.get(0) == 0){
            idEstimate(employeesalaryList,sheet);
        }else{
            idEstimate(employeesalaryList1,sheet);
        }
        try{
            response.addHeader( "Content-Disposition", "attachment;filename=employeesalay.xls");
            OutputStream out = response.getOutputStream();
            hwd.write(out);
            hwd.close();
            out.flush();
        }catch (IOException ioException){
            ioException.printStackTrace();
        }
    }




    private void idEstimate(List<Employeesalary> employeesalaryList, HSSFSheet sheet) {
        Employeesalary employeesalary;
        HSSFRow row;
        HSSFCell cell;
        if(employeesalaryList != null && employeesalaryList.size() > 0){
            for (int i = 0; i < employeesalaryList.size();i++){
                employeesalary = employeesalaryList.get(i);
                row = sheet.createRow(i+1);
                cell = row.createCell(0);
                cell.setCellValue(employeesalary.getId());
                cell = row.createCell(1);
                if(employeesalary.getEmpName() != null){
                    cell.setCellValue(employeesalary.getEmpName());
                }else {
                    cell.setCellValue("");
                }
                cell = row.createCell(2);
                if (employeesalary.getWorkID() != null){
                    cell.setCellValue(employeesalary.getWorkID());
                }else {
                    cell.setCellValue("");
                }
                cell = row.createCell(3);
                if (employeesalary.getMonthSalary() != null){
                    cell.setCellValue(employeesalary.getMonthSalary());
                }else {
                    cell.setCellValue("");
                }

            }
        }
    }
}

